function getBookList() {
    $.ajax({
        type: 'POST',
        url: '/reader/getBooksInfo',
        success: function (booksData) {
            var tbody = document.querySelector("#datatables tbody");
            tbody.innerHTML = "";
            var rows = booksData.map(function (books) {
                return `
            <tr>
                <td>${books.bookId || ""}</td>
                <td>${books.bookName || ""}</td>
                <td>${books.author || ""}</td>
                <td>${books.publication || ""}</td>
                <td>${books.publisher || ""}</td>
                <td>${books.typeId || ""}</td>
                <td>${books.price || ""}</td>
                <td>${books.bookSum || ""}</td>
            </tr>
        `;
            });
            tbody.innerHTML = rows.join("");
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    });
}

$("#showPopup").click(function () {
    $("#inputbookId").val("");
});

document.addEventListener("DOMContentLoaded", function () {
    var input = document.getElementById("inputreadId");
    $.ajax({
        url: "/reader/getReadId",
        type: "POST",
        success: function (readId) {
            input.value = readId;
        },
        error: function (error) {
            console.log("请求出错：", error);
        }
    });
});

$('#loginButton').click(function (event) {
    event.preventDefault();
    var readId = $('#inputreadId').val();
    var bookId = $('#inputbookId').val();
    $.ajax({
        type: 'POST',
        url: '/reader/borrowBook',
        contentType: 'application/x-www-form-urlencoded',
        data: { readId: readId, bookId: bookId },
        success: function (response) {
            if (response === '1') {
                alert("借书成功");
                window.location.href = "./borrowInfoHtml";
            } else if (response === '2') {
                alert("读者未办理借阅证");
                getBookList();
            } else if (response === '3') {
                alert("读者有罚款未交");
                getBookList();
            } else if (response === '4') {
                alert("读者借阅图书数量已达上限");
                getBookList();
            } else if (response === '5') {
                alert("图书库存不足");
                getBookList();
            }else {
                alert('未知的返回结果，请联系管理员！');
            }
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    });
});

$("#btn-query").click(function (event) {
    event.preventDefault();
    let tbody = document.querySelector("#datatables tbody");
    $.ajax({
        url : '/reader/findBookByInput',
        type : 'post',
        data : {
            bookId : $("#bookId").val(),
            bookName : $("#bookName").val(),
            author : $("#author").val(),
            publisher : $("#publisher").val(),
            typeId : $("#typeId").val(),
        },
        success: function(booksData){
            tbody.innerHTML = "";
            $.each(booksData, function (index, books) {
            var row = $("<tr></tr>");
            var bookIdTd = $("<td></td>").text(books.bookId);
            var bookNameTd = $("<td></td>").text(books.bookName);
            var bookAuthorTd = $("<td></td>").text(books.author);
            var bookpublication = $("<td></td>").text(books.publication);
            var bookpublisher = $("<td></td>").text(books.publisher);
            var booktypeId = $("<td></td>").text(books.typeId);
            var bookprice = $("<td></td>").text(books.price);
            var bookSum = $("<td></td>").text(books.bookSum);
            row.append(bookIdTd);
            row.append(bookNameTd);
            row.append(bookAuthorTd);
            row.append(bookpublication);
            row.append(bookpublisher);
            row.append(booktypeId);
            row.append(bookprice);
            row.append(bookSum);
            $("#datatables tbody").append(row);
            });
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    })
})

$("#btn-type-query").click(function (event) {
    event.preventDefault();
    let tbody = document.querySelector("#datatables1 tbody");
    $.ajax({
        url : '/reader/findBookTypeByInput',
        type : 'post',
        data : {
            typeId : $("#bookTypeId").val(),
            typeName : $("#typeName").val(),
        },
        success: function(bookTypeData){
            tbody.innerHTML = "";
            $.each(bookTypeData, function (index, bookType) {
                var row = $("<tr></tr>");
                var bookTypeIdTd = $("<td></td>").text(bookType.typeId);
                var bookTypeNameTd = $("<td></td>").text(bookType.typeName);
                row.append(bookTypeIdTd);
                row.append(bookTypeNameTd);
                $("#datatables1 tbody").append(row);
            });
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    })
})



